package com.dcrzl.gazgmh.module.mh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.dcrzl.gazgmh.common.entity.mh.MhBanner;
import com.dcrzl.gazgmh.common.entity.mh.MhMenu;
import com.dcrzl.gazgmh.common.entity.mh.MhModule;
import com.dcrzl.gazgmh.common.mapper.mh.MhModuleMapper;
import com.dcrzl.gazgmh.common.service.impl.BaseServiceImpl;
import com.dcrzl.gazgmh.common.utils.PageInfo;
import com.dcrzl.gazgmh.common.utils.Result;
import com.dcrzl.gazgmh.common.utils.SysConstant;
import com.dcrzl.gazgmh.module.mh.service.MhBannerService;
import com.dcrzl.gazgmh.module.mh.service.MhMenuService;
import com.dcrzl.gazgmh.module.mh.service.MhModuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;
@Slf4j
@Service("mhModuleService")
public class MhModuleServiceImpl extends BaseServiceImpl<MhModuleMapper, MhModule> implements MhModuleService {

    @Autowired
    private MhMenuService mhMenuService;

    @Autowired
    private MhBannerService mhBannerService;

    private LambdaQueryWrapper<MhModule> commonWrapper(MhModule mhModule) {
        LambdaQueryWrapper<MhModule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(mhModule.getStatus()), MhModule::getStatus, mhModule.getStatus());
        wrapper.eq(Objects.nonNull(mhModule.getType()), MhModule::getType, mhModule.getType());
        wrapper.like(StringUtils.hasText(mhModule.getName()), MhModule::getName, mhModule.getName());
        if (StringUtils.hasText(mhModule.getTypeListStr())) {
            wrapper.in(MhModule::getType, Arrays.asList(mhModule.getTypeListStr().split(",")));
        }
        wrapper.in(!CollectionUtils.isEmpty(mhModule.getIdList()),MhModule::getId, mhModule.getIdList());
        return wrapper;
    }

    @Override
    public Result<MhModule> selectOne(MhModule mhModule) {
        LambdaQueryWrapper<MhModule> wrapper = this.commonWrapper(mhModule);
        return super.selectOne(wrapper);
    }

    @Override
    public Result<List<MhModule>> selectList(MhModule mhModule) {
        LambdaQueryWrapper<MhModule> wrapper = this.commonWrapper(mhModule);
        return super.selectList(wrapper);
    }

    @Override
    public PageInfo<MhModule> selectPage(MhModule mhModule) {
        LambdaQueryWrapper<MhModule> wrapper = this.commonWrapper(mhModule);
        return super.selectPage(mhModule, wrapper);
    }

    @Override
    public PageInfo<MhModule> selectUPage(MhModule mhModule) {
        PageInfo<MhModule> mhModulePageInfo = this.selectPage(mhModule);
        List<MhModule> list = mhModulePageInfo.getList();
        Set<Long> menuIdSet = new HashSet<>();
        list.forEach(mhModule1 -> {
            menuIdSet.add(mhModule1.getLinkId());
        });
        if (!CollectionUtils.isEmpty(menuIdSet)) {
            Result<List<MhMenu>> listResult = mhMenuService.selectList(new MhMenu().setIdList(menuIdSet));
            if (listResult.isSuccess()) {
                List<MhMenu> mhMenuList = listResult.getData();
                Map<Long, MhMenu> map = new HashMap<>();
                mhMenuList.forEach(mhMenu1 -> {
                    map.put(mhMenu1.getId(), mhMenu1);
                });
                list.forEach(mhModule1 -> {
                    mhModule1.setMhMenu(map.get(mhModule1.getLinkId()));
                });
            }
        }
        return mhModulePageInfo;
    }

    @Override
    public Result<List<MhModule>> selectUList(MhModule mhModule) {
        Result<List<MhModule>> listResult = this.selectList(mhModule);
        if (listResult.isSuccess()) {
            List<MhModule> list = listResult.getData();
            Set<Long> menuIdSet = new HashSet<>();
            List<Long> idList = new ArrayList<>();
            list.forEach(mhModule1 -> {
                if(mhModule1.getType()==0||mhModule1.getType()==1){
                    idList.add(mhModule1.getId());
                }
                if(mhModule1.getType()==3){
                    menuIdSet.add(mhModule1.getLinkId());
                }
            });
           if(!CollectionUtils.isEmpty(idList)){
               Result<List<MhBanner>> listResult1 = mhBannerService.selectList(new MhBanner().setModuleIdList(idList));
               if (listResult1.isSuccess()) {
                   List<MhBanner> mhBannerList = listResult1.getData();
                   MultiValueMap<Long, MhBanner> map = new LinkedMultiValueMap<>();
                   mhBannerList.forEach(banner -> {
                       map.add(banner.getModuleId(), banner);
                   });
                   list.forEach(mhModule1 -> {
                       mhModule1.setBannerList(map.get(mhModule1.getId()));
                   });
               }
           }
            if(!CollectionUtils.isEmpty(menuIdSet)){
                Result<List<MhMenu>> listResult1 = mhMenuService.selectList(new MhMenu().setIdList(menuIdSet));
                if (listResult1.isSuccess()) {
                    List<MhMenu> mhMenuList = listResult1.getData();
                    Map<Long, MhMenu> map = new HashMap<>();
                    mhMenuList.forEach(mhMenu1 -> {
                        map.put(mhMenu1.getId(), mhMenu1);
                    });
                    list.forEach(mhModule1 -> {
                        mhModule1.setMhMenu(map.get(mhModule1.getLinkId()));
                    });
                }
            }
        }
        return listResult;
    }

    @Override
    public Result<Integer> removeById(Serializable id) {
        MhModule mhModule = new MhModule().setStatus(SysConstant.DATA_DELETE_STATUS)
                .setId((Long) id);
        return this.updateById(mhModule);
    }

}